/**
  ******************************************************************************
  * @file    :MG2xx-test.c
  * @author  :MG Team
  * @version :V1.0
  * @date
  * @brief
  ******************************************************************************
***/
#include "Includes.h"

/*************************************************
Functions used for SRRC/FCC test
Called after BLE_Init()
Add while(1){} after calling
*************************************************/


//FCC/SRRC Carrier test
void SRCC_CarrierTest(unsigned char chan) //N=(F-2402)/2
{
    u8 tmp[3] = {0x08,0xc0,0x80|chan};

    MG_Write_Buffer(CONT_TEST_MODE,tmp,3);

    MG_Write_Reg(XO_PD_EN, 0x10); //cont_wave=1,carrier_wave_en=0
    Delay_ms(5);

    MG_Write_Reg(WAKEUP_NOW, 1);
}

#define PKT_PAYLOAD_PRBS9     0
#define PKT_PAYLOAD_11110000  1
#define PKT_PAYLOAD_10101010  2
#define PKT_PAYLOAD_PRBS15    3
#define PKT_PAYLOAD_11111111  4
#define PKT_PAYLOAD_00000000  5
#define PKT_PAYLOAD_00001111  6
#define PKT_PAYLOAD_01010101  7
u8 AA_BQB_DTM[4]= {0x29, 0x41, 0x76, 0x71};
void SRRC_PRBS9Test(unsigned char chan, unsigned char pkt_pld, unsigned char pkt_len) //N=(F-2402)/2
{
    u8 tmp[5] = {0x48,0xc0,0x80|chan,0x00,0x80|pkt_pld};

    MG_Write_Buffer(CONT_TEST_MODE,tmp,5);

    tmp[0] = 0x83;   //scramble=0
    tmp[1] = AA_BQB_DTM[0];
    tmp[2] = AA_BQB_DTM[1];
    tmp[3] = AA_BQB_DTM[2];
    tmp[4] = AA_BQB_DTM[3];
    MG_Write_Buffer(CONFIG, tmp, 5);

    //crc init=0x555555

    MG_Write_Reg(XO_PD_EN, 0x00); //cont_wave=0,carrier_wave_en=0
    Delay_ms(5);

    //625us: if len=37~115,  625*2: if len=116~255
    tmp[0] = 0x71; //0x00271=625
    tmp[1] = 2;
    tmp[2] = 0;
    tmp[3] = 1;   //RC_LOAD;
    MG_Write_Buffer(ADV_INTERV_RC_CFG,tmp,4);

    tmp[0] = 0x08;
    tmp[1] = 0;
    tmp[2] = 0;
    tmp[3] = pkt_pld;
    tmp[4] = pkt_len; //length
    MG_Write_Buffer(ADV_PKT_CFG, tmp, 5);

    MG_Write_Reg(BASEBAND_CFG, 1); //SLP_XO=4, STOP=2, WKUP_EN=1

    MG_Write_Reg(WAKEUP_NOW, 1);
}
